package com.cyzy.controller;

import com.alibaba.fastjson.JSON;
import com.cyzy.dto.ProductWithBanner;
import com.cyzy.dto.Result;
import com.cyzy.pojo.Banner;
import com.cyzy.service.BannerService;
import com.cyzy.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/product/home")
public class HomeController {
    @Autowired
    private BannerService bannerService;
    @Autowired
    private ProductService productService;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @GetMapping("/banner")
    public Result banner() {

        if (redisTemplate.opsForValue().get("Product_homepage_banner") == null) {
            List<Banner> list = new ArrayList<>();
            list = bannerService.getBannerList();
            redisTemplate.opsForValue().set("product:Product_homepage_banner", JSON.toJSONString(list));
            return Result.ok().data("bannerList", list);
        } else {
            return Result.ok().data("bannerList", JSON.parse(redisTemplate.opsForValue().get("product:Product_homepage_banner")));
        }
    }
    @GetMapping("/productList")
    public Result productList(){
        if (redisTemplate.opsForValue().get("product:Product_homepage_productList") == null){
            List<ProductWithBanner> list = new ArrayList<>();
            list = productService.getProducts(null);
            redisTemplate.opsForValue().set("product:Product_homepage_productList", JSON.toJSONString(list));
            return Result.ok().data("productList", list);
        }else{
            return Result.ok().data("productList", JSON.parse(redisTemplate.opsForValue().get("product:Product_homepage_productList")));
        }
    }

    @GetMapping("/productInfo/{productId}")
    public Result productInfo(@PathVariable(name = "productId") Integer productId){
        if(redisTemplate.opsForValue().get("product:Info:id="+productId) != null){
            return Result.ok().data("product",JSON.parse(redisTemplate.opsForValue().get("product:Info:id="+productId)));
        }
        if (redisTemplate.opsForValue().get("product:Product_homepage_productList") == null){
            List<ProductWithBanner> list = new ArrayList<>();
            list = productService.getProducts(productId);
            redisTemplate.opsForValue().set("product:Info:id="+productId, JSON.toJSONString(list));
            return Result.ok().data("product", list);
        }else{
            List<ProductWithBanner> list = new ArrayList<>();
            List<ProductWithBanner> product = new ArrayList<>();
            list=JSON.parseArray(redisTemplate.opsForValue().get("product:Product_homepage_productList"),ProductWithBanner.class);
            System.out.println("*********************************"+list);
            Boolean flag=false;
            for(ProductWithBanner p:list){
                if(p.getProductId()==productId){
                    flag=true;
                    product.add(p);
                }
            }
            if(flag){
                redisTemplate.opsForValue().set("product:Info:id="+productId, JSON.toJSONString(product));
                return Result.ok().data("product", product);
            }
            List<ProductWithBanner> newlist = new ArrayList<>();
            newlist = productService.getProducts(productId);
            redisTemplate.opsForValue().set("product:Info:id="+productId, JSON.toJSONString( newlist));
            return Result.ok().data("product", newlist);
        }
    }
}

